package com.scau.yangsuiyu.estore.controller.myComponent;

import com.alibaba.fastjson.JSONObject;
import com.scau.yangsuiyu.estore.dababases.entity.Buyer;
import com.scau.yangsuiyu.estore.dababases.entity.User;
import com.scau.yangsuiyu.estore.service.AppService;
import com.scau.yangsuiyu.estore.service.CartService;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 顶部导航栏
 * @date 2019-05-23 21:33
 * @email suiyu_yang@163.com
 */
public class TopSpan extends AnchorPane implements Initializable {

    @FXML
    private Button gotoLoginButton;
    @FXML
    private Button gotoRegisterButton;
    @FXML
    private Button gotoUserLayoutButton;
    @FXML
    private Button gotoCartButton;
    @FXML
    private Button gotoIndexButton;
    @FXML
    private Button logoutButton;

    private User user = null;

    public TopSpan() {
        loadFXML();
    }

    public TopSpan(User user) {
        this.user = user;
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/myComponent/TopSpan.fxml"));
        fxmlLoader.setController(this);
        fxmlLoader.setRoot(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        if (user != null) {
            gotoLoginButton.setText("欢迎你，" + user.getUsername());
            logoutButton.setVisible(true);
        } else {
            gotoLoginButton.setText("亲，请登录！");
            logoutButton.setVisible(false);
        }
    }

    @FXML
    private void gotoLogin() {
        if (user == null) {
            new AppService().closeCurrentTab();
            new AppService().gotoLogin();
        } else {
            gotoUserLayout();
        }
    }

    @FXML
    private void gotoRegister() {
        new AppService().gotoRegister();
    }

    @FXML
    private void gotoCart() {
        if (!(user instanceof Buyer)) {
            new Alert(Alert.AlertType.ERROR, "请以买家身份登录！").showAndWait();
            new AppService().closeCurrentTab();
            new AppService().gotoLogin();
        } else {
            new AppService().gotoCartPane((Buyer) user);
        }
    }

    @FXML
    private void gotoIndex() {
        new AppService().gotoIndexPage();
    }

    @FXML
    private void gotoUserLayout() {
        if (user == null) {  //当前并未登录，自动转去登录界面
            new AppService().gotoLogin();
        } else { //当前已登录，转去用户管理界面
            JSONObject userJson = new JSONObject();
            userJson.put("user", user);
            if (user instanceof Buyer) {
                new AppService().gotoBuyerLayout(userJson);
            } else {
                new AppService().gotoSellerLayout(userJson);
            }
        }
    }

    @FXML
    private void logout() {
        user = null;
        new AppService().closeCurrentTab();
        gotoIndex();
    }

    public User getUser() {
        return user;
    }
}
